// +----------------------------------------------------------------------
// | CmsWing [ 美业管理系统 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 psi All rights reserved.
// +----------------------------------------------------------------------
// | Author: huang <2374266244@qq.com>
// +----------------------------------------------------------------------
'use strict';
import Base from './base.js';

export default class extends Base {
	// 获取品牌列表
	async productlistAction(){
		let page = this.post();
		let pages = page.pages;
		let limits = page.limits;
		let search = page.search;
		let zt = page.zt;
		let shopid = this.userinfo.departmentid;

		let data = await this.model('or_goods')
				.alias('g')
				.field('g.*,b.name as bname,c.name as cname')
				.join('`think_or_brand` b on b.id=g.brandid')
				.join('`think_or_subjectcategory` c on c.id=g.categoryid')
				.page(pages, limits)
				.order('g.id DESC')
				.where({'g.departmentid':shopid, 'g.isdisabled':zt, 'g.name|g.number':['like','%'+search+'%']})
				.countSelect();

		return this.json(data);
	}

	// 获取产品对应的品牌列表
	async getbrandAction(){
		if (this.isPost) {
			let belongsid = JSON.parse(this.post('id'));
			let data = [];
			if (!think.isEmpty(belongsid)) {
				data = await this.model('or_brand').where({id:['IN',belongsid]}).select();
			}
			return data?this.json(data):this.fail({info:'无品牌'});
		}else{
			let data = await this.model('or_brand').where({departmentid:this.userinfo.departmentid}).select();
			return this.json(data);
		}
	}

	// 获取产品机构分类和品牌
	async getbelongsAction(){
		let shopid = this.userinfo.departmentid;
		let catelist = await this.model().query('select * from `think_or_subjectcategory` where departmentid='+shopid+' and pid!=0');
		let brandlist = await this.model().query('select * from `think_or_brand` where departmentid='+shopid);
		let data = {
			cl : catelist,
			bl : brandlist
		}
		return this.json(data);
	}

	// 停用、启用产品
	async goodsstatusAction(){
		let id = this.post('id');
		let st = this.post('st');
		let laqu = await this.ispull(id);
		if (!think.isEmpty(laqu)) {
			return this.fail(laqu.msg)
		}
		let res = await this.model('or_goods').where({id:id}).update({isdisabled:st});
		if (st*1) {
			return res?this.success('停用成功'):this.fail('停用失败');
		} else {
			return res?this.success('启用成功'):this.fail('启用失败');
		}
	}

	// 添加/编辑产品
	async addeditAction(){
		let data = this.post();
		let orid = this.userinfo.departmentid;
		let addtime = Math.round(new Date().valueOf()/1000);
		
		// 判断产品名称是否重复
		let repeat = await this.model('or_goods').where('departmentid="'+orid+'" AND (name="'+data.name+'" OR number="'+data.number+'") AND id!="'+data.id+'"').select();

		if (!think.isEmpty(repeat)) {
			return this.fail('名称或编号重复');
		}

		if (data.tdtype==1) {
			data.percent = Number(data.percent/100).toFixed(2);
			data.fixed = 0;
		} else {
			data.percent = 0;
			data.fixed = data.fixed;
		}

		if (data.khtdtype==1) {
			data.khpercent = Number(data.khpercent/100).toFixed(2);
			data.khfixed = 0;
		} else {
			data.khpercent = 0;
			data.khfixed = data.khfixed;
		}

		if (data.id*1) {
			/*let laqu = await this.ispull(data.id);

			if (!think.isEmpty(laqu)) {
				return this.fail(laqu.msg)
			}*/

			let upd = await this.model('or_goods').update(data);
			return upd?this.success('编辑成功'):this.fail('编辑失败');
		}

		let add = {
			name : data.name,
			price : data.price,
			remarks : data.remarks,
			departmentid : orid,
			addtime : addtime,
			brandid : data.brandid,
			categoryid: data.categoryid,
			nums: data.nums,
			number: data.number,
			tdtype: data.tdtype,
			percent: data.percent,
			fixed: data.fixed,
			khtdtype: data.khtdtype,
			khpercent: data.khpercent,
			khfixed: data.khfixed,
		}

		let res = await this.model('or_goods').add(add);
		
		return res?this.success('添加成功'):this.fail('添加失败');
	}

	// 其他店家选择机构商品
	async selectgoodsAction(){
		let click = await this.model('or_goods')
			.alias('g')
			.field('g.*, b.name as bname')
			.join('`think_or_brand` b on b.id=g.brandid')
			.where({'g.departmentid':this.userinfo.departmentid, 'g.isdisabled':0})
			.select();
		let relist = {
			"draw": '',
			"data": clist,
			"breadcrumb":''
		}
		return this.json(relist);
	}

	// 如果分店拉取机构产品，则机构产品不能操作
	async ispull(goodsid){
		let isuse = await this.model('or_shop_goods').where({goodsid: goodsid}).select();
		if (!think.isEmpty(isuse)) {
			return {msg: '分店拉取的产品不能操作'};
		}
	}
}